package com.fare.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XmlUtil {
	
	public static String getXML(Map<String,List<String>> airPrice,List<String> dates,String tableName){
		String	XML = "<?xml version='1.0'?>" +
		"<chart caption='" +tableName + "' subCaption='For the year 2012' numberPrefix='￥' " +
		"sformatNumberScale='10' sNumberPrefix='￥' syncAxisLimits='1' rotateValues='0' showSum='0'><categories>";
		for(String s:dates){
			XML += "<category label='"+s.substring(5)+"'/>";
		}
		XML += "</categories>";
		for(String key: airPrice.keySet()){
			XML += "<dataset seriesName='"+key+"' anchorRadius='5'>";
			for(String price :airPrice.get(key)){
				XML += "<set value='"+price+"'/>";
			}
			XML += "</dataset>";
		}
			XML += "<lineset seriesName='Target Profitability' valuePosition='BELOW'>"+
			"	<set value='500'/>"+
			"<set value='700'/>"+
			"<set value='900'/>"+
			"<set value='1100'/>"+
			"<set value='1300'/>"+
		"</lineset>"+
	"</chart>";
		return XML;
	}
	
	public static String getXML(Map<String,Map<String,String>> airPrice,List<String> dates,List<String> airCompanies,String tableName){
		String	XML = "<?xml version='1.0'?>" +
		"<chart caption='" +tableName + "' subCaption='For the year 2012' numberPrefix='￥' " +
		"sformatNumberScale='10' sNumberPrefix='￥' syncAxisLimits='1' rotateValues='0' showSum='0'><categories>";
		for(String s:dates){
			XML += "<category label='"+s.substring(5)+"'/>";
		}
		XML += "</categories>";
		Map<String, List<String>> priceOfAirCompany = new HashMap<String, List<String>>();//航空公司为key的价格列表(对应日期)
		for(String date:dates){//重新组织数据结构
			for(String priceKey: airPrice.get(date).keySet()){
				if(priceOfAirCompany.get(priceKey) == null){
					priceOfAirCompany.put(priceKey, new ArrayList<String>());//新增未添加的航空公司
				}
				priceOfAirCompany.get(priceKey).add(airPrice.get(date).get(priceKey));
			}
		}
		for(String companyName: airCompanies){
			XML += "<dataset seriesName='"+companyName+"' anchorRadius='5'>";
			for(String price : priceOfAirCompany.get(companyName)){
				XML += "<set value='"+price+"'/>";
			}
			XML += "</dataset>";
		}
			XML += "<lineset seriesName='Target Profitability' valuePosition='BELOW'>"+
			"	<set value='500'/>"+
			"<set value='700'/>"+
			"<set value='900'/>"+
			"<set value='1100'/>"+
			"<set value='1300'/>"+
		"</lineset>"+
	"</chart>";
		return XML;
	}
}
